package com.wanyang.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

    public static Integer currentUnixTime() {
        return new Long(System.currentTimeMillis() / 1000L).intValue();
    }

    public static String handleTime(int unixTime) {
        int current = currentUnixTime();

        if (current - unixTime < 60) {
            return "几秒前";

        } else if (current - unixTime < (60*60)) {
            return (current - unixTime) / 60 + "分钟前";

        } else if (current - unixTime < (24*60*60) ) {
            return (current - unixTime) / (60*60) + "小时前";

        } else if (current - unixTime < (7*24*60*60) ) {
            return (current - unixTime) / (24*60*60) + "天前";

        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.format(new Date(unixTime * 1000L));
        }

    }

    public static List<String> getThisWeekDates(Date date) {
        return getThisWeekDates(new SimpleDateFormat("yyyyMMdd"), date);
    }

    public static List<String> getThisWeekDates(SimpleDateFormat sdf, Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        String beginDate = sdf.format(cal.getTime());
        List<String> result = new ArrayList<>();
        result.add(beginDate);

        for (int i=1;i<=6;i++) {
            cal.add(Calendar.DATE, 1);
            result.add(sdf.format(cal.getTime()));
        }

        return result;
    }

    public static void main(String[] args) {
        System.out.println(handleTime(1604036665));
    }

}
